package com.fenghuajueli.libbasecoreui.utils;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;

import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.FileNameMap;
import java.net.URLConnection;

public class AlbumUtils {
    //保存图片
//    public static void scanFile(File file){
//        String mimeType = getMimeType(file);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
//            String fileName = file.getName();
//            ContentValues values = new ContentValues();
//            values.put(MediaStore.MediaColumns.DISPLAY_NAME,fileName);
//            values.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
//            values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DCIM);
//            ContentResolver contentResolver = Utils.getApp().getContentResolver();
//            Uri uri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
//            if(uri == null){
//                ToastUtils.showShort("图片保存失败");
//                return;
//            }
//            try {
//                OutputStream out = contentResolver.openOutputStream(uri);
//                FileInputStream fis = new FileInputStream(file);
//                FileUtils.copy(fis,out);
//                fis.close();
//                out.close();
//                ToastUtils.showShort("图片保存成功");
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }else {
//            MediaScannerConnection.scanFile(Utils.getApp(), new String[]{file.getPath()}, new String[]{mimeType}, (path, uri) -> {
//                ToastUtils.showShort("图片已成功保存到" + path);
//            });
//        }
//    }


    public static String getMimeType(File file){
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String type = fileNameMap.getContentTypeFor(file.getName());
        return type;
    }
}
